package com.fmjava.service_video.controller;


import com.fmjava.service_base.exception.MyException;
import com.fmjava.service_video.entity.Chapter;
import com.fmjava.service_video.entity.Video;
import com.fmjava.service_video.entity.vo.ChapterVO;
import com.fmjava.service_video.feignClient.VideoClient;
import com.fmjava.service_video.service.ChapterService;
import com.fmjava.service_video.service.VideoService;
import com.fmjava.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 作品章节 前端控制器
 * </p>
 *
 * @author fmjava
 * @since 2021-07-19
 */
@RestController
@RequestMapping("/service_video/chapter")
@Api(tags = "章节视频上传组")
public class ChapterController {
    @Autowired
    ChapterService chapterService;

    @Autowired
    VideoService VideoService;
    @PostMapping("/addChapter")
    public ResponseResult addChapter(@RequestBody Chapter chapter) {
        chapterService.save(chapter);
        return ResponseResult.ok();
    }

    @ApiOperation("查询所有的章节列表")
    @GetMapping("/getChapterContentVideo/{contentId}")
    public ResponseResult getChapterContentVideo(@ApiParam(name = "contentId",value ="作id",required = true)@PathVariable("contentId") String contentId){
        List<ChapterVO> chapterContentVideo = chapterService.getChapterContentVideo(contentId);
        return ResponseResult.ok().data("items",chapterContentVideo);
    }

    @ApiOperation("获取作品记录")
    @GetMapping("/getOneChapter/{id}")
    public ResponseResult getOneChapter(@PathVariable("id") String id){
        Chapter chapter = chapterService.getById(id);
        return ResponseResult.ok().data("item",chapter);
    }

    @ApiOperation("更新章节")
    @PostMapping("updateChapter")
    public ResponseResult updateChapter(@RequestBody Chapter chapter){
        chapterService.updateById(chapter);
        return ResponseResult.ok();
    }

    @PostMapping("/deleteChapterWithId/{id}")
    @ApiParam(value = "删除章节")
    public ResponseResult deleteChapterWithId(@PathVariable("id") String id){
        List<Video> videoList = VideoService.getVideoWithContentId(id);
        System.out.println(videoList.size());
        if (videoList.size()!=0) {

            throw new MyException(20001,"该章节下存在小节，请先删除小节");
        }
        chapterService.removeById(id);
        return ResponseResult.ok();
    }
}

